using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

using IGlobalist.LectureService;

namespace IGlobalist
{
    public partial class FrmLogin : Form
    {
        Service svc = Global.Svc;
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if("".Equals(txtPassword.Text)||"".Equals(txtUserName.Text)){
                    lblError.Text = "Username and password are required";
                    txtUserName.Focus();
                    return;
            }
            btnLogin.Enabled = false;
            svc.Url = txtServiceAddress.Text;
            Global.LocalIPAddress =  (IPAddress) cboIPs.SelectedItem;
            try
            {
                ClientDescription desc = svc.Login(txtUserName.Text, txtPassword.Text);
                if (!desc.IsLogin)
                {
                    lblError.Text = "Wrong username or password";
                    txtUserName.Focus();
                    btnLogin.Enabled = true;
                }
                else
                {
                    Global.SourceDescription = desc;
                    CastServiceDescription srvDesc = svc.GetCastServiceDesc();
                    if (desc.IsMainSource)
                    {
                        Global.ServiceStarted = false;
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        if (srvDesc.ServiceStarted)
                        {
                            Global.CastServiceDescription = srvDesc;
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            lblError.Text = "Login OK but the service's not started yet. Please wait";
                            timer.Enabled = true;
                        }
                    }
                }
            }
            catch
            {
                lblError.Text = "Request service errors. Please check service address";
                btnLogin.Enabled = true;
            }
        }

        private void txtUserName_Validating(object sender, CancelEventArgs e)
        {
            if ("".Equals(txtUserName.Text))
            {
                errorProvider.SetError(txtUserName, "Username is required");
                txtUserName.Focus();
            }
            else
                errorProvider.SetError(txtUserName, "");
        }

        private void txtPassword_Validating(object sender, CancelEventArgs e)
        {
            if ("".Equals(txtPassword.Text))
            {
                errorProvider.SetError(txtPassword, "Password is required");
                txtPassword.Focus();
            }
            else
                errorProvider.SetError(txtPassword, "");
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            progressBar.Visible = true;

            CastServiceDescription srvDesc = svc.GetCastServiceDesc();
            if (srvDesc.ServiceStarted)
            {
                Global.ServiceStarted = true;
                Global.CastServiceDescription = srvDesc;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                this.Invoke(new UpdateProcess(UpdateProgressBar));
            }
        }
        private delegate void UpdateProcess();
        private int processing = 0;
        public void UpdateProgressBar()
        {
            processing += 5;
            if (processing > 100) processing = 0;
            progressBar.Value = processing;
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            txtServiceAddress.Text = global::IGlobalist.Properties.Settings.Default.WindowClient_LectureService_Service;
            foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (!IPAddress.IsLoopback(ip) && ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    cboIPs.Items.Add(ip);
                }
            }
            cboIPs.SelectedIndex = 0;
        }
    }
}